package com.zys.el.user.controller;

import com.zys.el.core.controller.BaseController;
import com.zys.el.core.po.ResponseBean;
import com.zys.el.user.client.AnticleClient;
import com.zys.el.user.config.po.New;
import com.zys.el.user.config.po.UserNote;
import com.zys.el.user.po.Note;
import com.zys.el.user.po.User;
import com.zys.el.user.service.IFieldService;
import com.zys.el.user.service.INoteService;
import com.zys.el.user.service.IUserService;
import com.zys.el.user.util.Analyzer.AnalyzerUtil;
import com.zys.el.user.util.FieldUtil;
import com.zys.el.user.util.HLightText.LightTextUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/note")
public class NoteController extends BaseController<INoteService, Note> {

    @Autowired
    private IUserService userService;
    @Autowired
    private AnalyzerUtil analyzerUtil;
    @Autowired
    private FieldUtil fieldUtil;
    @Autowired
    private IFieldService iFieldService;
    @Autowired
    private LightTextUtil lightTextUtil;
    @Autowired
    private AnticleClient anticleClient;

    @ApiOperation(value = "查询所有", notes = "")
    @GetMapping("/list-all")
    public List<Note> selectAll() {
        return service.getListHaveDelete();
    }


    @GetMapping("/count/{id}")
    public Integer GetNoteCount(@PathVariable Long id) {
        return service.getNoteCount(id);
    }

    @GetMapping("/get_new")
    public ResponseBean getNote() {
        List<New> result = new ArrayList<>();

        List<Note> list = service.getListHaveDelete();
        for (Note n : list) {
//            System.out.println(n.getAuthorId());
            if (n.getAuthorId() != null) {
                User edit = userService.getById(n.getAuthorId());
                if (edit != null) {

                    result.add(New.builder().id(n.getId())
                            .title(n.getItem())
                            .content(n.getNoteItem())
                            .user_image(edit.getImage())
                            .user_name(edit.getName()).build());
                }

            }

        }
        ResponseBean responseBean = new ResponseBean();
        responseBean.setToken("查询成功");
        responseBean.setModel(result);
        return responseBean;
    }


    @GetMapping("/get_note/{id}")
    public ResponseBean getEditNoteById(@PathVariable Long id) {
//        System.out.println(id);

        Note note = service.getById(id);
//        Random r = new Random();
//        int i = r.nextInt(2);
//        if (i == 0) {
//            note.setContentPath(lightTextUtil.LeftFightWord(note.getContentPath()));
//        } else if (i == 1) {
//            note.setContentPath(lightTextUtil.headText(note.getContentPath()));
//        } else {
//            note.setContentPath(lightTextUtil.ForthCi(note.getContentPath()));
//        }

        note.setContentPath(lightTextUtil.headText(note.getContentPath()));

        if (note == null) return null;
//        System.out.println(note);
        User user = new User();
        if (note.getAuthorId() != null) {
            user = userService.getById(note.getAuthorId());
        }
        UserNote result = new UserNote(note.getItem(), user.getName(), user.getImage(), "云习-学有所获", note.getContentPath());
        ResponseBean responseBean = new ResponseBean();
        responseBean.setModel(result);
        return responseBean;
    }

    @ApiOperation(value = "保存", notes = "ID存在修改，不存在添加")
    @PostMapping("/save")
    @Override
    public ResponseBean save(@RequestBody Note entity) throws IOException {
        ResponseBean rm = new ResponseBean();
        if (entity.getAuthorId() == null) {
            rm.setSuccess(false);
            rm.setMsg("不可避免异常：用户id为空");
            rm.setCode(500);
            return rm;
        }
        if (entity.getItem().equals("") || entity.getItem() == null) {
            rm.setSuccess(false);
            rm.setMsg("不可避免异常：笔记主题为空");
            rm.setCode(500);
            return rm;
        }

        if (entity.getNoteItem().equals("") || entity.getNoteItem() == null) {
            rm.setSuccess(false);
            rm.setMsg("不可避免异常：无笔记摘要参数");
            rm.setCode(500);
            return rm;
        }
        if (entity.getContentPath().equals("") || entity.getContentPath() == null) {
            rm.setSuccess(false);
            rm.setCode(500);
            rm.setMsg("不可避免异常：内容不可为空");
            return rm;
        }
//        if (entity.getFields().size() == 0 || entity.getFields() == null) {
//            rm.setSuccess(false);
//            rm.setCode(500);
//            rm.setMsg("不可避免异常：领域不可为空");
//            return rm;
//        }

        try {
            entity.setCreateTime(new Date());
            if (entity.getAuthorId() == null) {
                entity.setAuthorId(1l);
            }

            service.saveOrUpdate(entity);
//            System.out.println(entity);
            entity.setDelete(0);

            anticleClient.addNote(entity);

            rm.setModel(entity);

            if (null == entity.getFields() || entity.getFields().size() == 0) {
                String item = entity.getItem();
                Set<String> ik = analyzerUtil.ik(item);
//                System.out.println(ik);
                List<String> ikList = new ArrayList<>();

                Iterator<String> iterator = ik.iterator();
                while (iterator.hasNext()) {
                    String next = iterator.next();
//                    System.out.println(next);
                    ikList.add(next);
                }
//                System.out.println("ikList:" + ikList);
                if (ikList != null || ikList.size() == 0) {
//                    System.out.println("----------");
                    fieldUtil.FieldHandle(ikList, entity.getId());
                }
                rm.setMsg("系统识别领域为:" + ikList);
            } else {
                fieldUtil.FieldHandle(entity.getFields(), entity.getId());
                rm.setMsg("系统采用了用户输入的相关领域");
            }

        } catch (Exception e) {
            e.printStackTrace();
            rm.setSuccess(false);
            rm.setMsg("保存失败");
            rm.setCode(500);
        }
        return rm;
    }

    //  删除笔记
    @DeleteMapping("/delete/{nid}")
    public ResponseBean delteNoteById(@PathVariable Long nid) {
//        System.out.println("==========");
        Note byId = service.getById(nid);
        ResponseBean rn = new ResponseBean();
        if (byId == null) {
            rn.setMsg("用户不存在");
            rn.setCode(500);
            rn.setSuccess(false);
            return rn;
        }
        byId.setDelete(1);
        boolean b = service.saveOrUpdate(byId);
        anticleClient.deleteNoteById(byId.getId());

        if (b == false) {
            rn.setMsg("删除错误");
            rn.setCode(500);
            rn.setModel(b);
            rn.setSuccess(false);
        } else {

            rn.setMsg("成功删除");
            rn.setCode(200);
            rn.setModel(b);
            rn.setSuccess(true);
        }
        return rn;
    }
}
